package client.GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

public class Userlogin extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    private static final JFrame jf = new JFrame();//登陆界面

    JTextField jtf = new JTextField();//用户名数组

    //登陆界面
    ImageIcon i3 = new ImageIcon("C:/Users/lixiang/Desktop/images/login_hover.jpg");
    JButton jb = new JButton("登       陆", i3);


    //登陆界面,调用就出来，使用无参构造
    public Userlogin() {
        jf.setSize(426, 300);//窗口大小
        jf.setLocation(497, 242);//显示在屏幕的位置
        jf.setUndecorated(true);//窗口边框不显示
        jf.setResizable(false);//固定窗口大小
        jf.setLayout(null);//取消默认布局

        //登陆界面
        /*
            顶部
         */
        JPanel p1 = new JPanel();
        p1.setLayout(null);//取消默认布局
        p1.setBackground(Color.white);
        p1.setPreferredSize(new Dimension(0, 135));//设置面板尺寸
        //背景图片
        ImageIcon i1 = new ImageIcon("C:/Users/lixiang/Desktop/images/back.jpg");//添加背景图片
        JLabel background1 = new JLabel(i1);
        background1.setBounds(-7, 0, 440, i1.getIconHeight());//设置背景图片位置，尺寸
        p1.add(background1);//背景图片加入到面板中
        //顶部面板的退出按钮
        JButton out = new JButton(new ImageIcon("C:/Users/lixiang/Desktop/images/close2_normal.jpg"));
        out.setBounds(402, 0, 25, 26);//退出按钮位置
        out.setRolloverIcon(new ImageIcon("C:/Users/lixiang/Desktop/images/close_hover.jpg"));//鼠标移动时更改图标为红×
        out.setBorderPainted(false);//取消按钮边框效果
        //监听关闭按钮
        out.addActionListener(e -> System.exit(0));
        p1.add(out);


        /*
            左侧
         */
        JPanel p2 = new JPanel();
        p2.setBackground(Color.white);
        p2.setLayout(null);
        p2.setPreferredSize(new Dimension(130, 0));
        //左侧添加背景图片
        ImageIcon i2 = new ImageIcon("C:/Users/lixiang/Desktop/images/qq.jpg");
        JLabel background2 = new JLabel(i2);
        background2.setBounds(0, 10, 120, 110);
        p2.add(background2);


        /*
            中部
         */
        JPanel p3 = new JPanel();
        p3.setBackground(Color.white);
        p3.setLayout(null);
        JLabel jLabel = new JLabel("请输入您的用户名");
        jLabel.setBounds(0, 15, 150, 50);
        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        p3.add(jLabel, BorderLayout.NORTH);//加入面板


        jtf.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        jtf.setBounds(0, 70, 175, 30);
        jtf.setPreferredSize(new Dimension(185, 25));
        p3.add(jtf);//加入面板


        /*
            右侧
         */
        //总面板
        JPanel p4 = new JPanel();
        p4.setLayout(null);
        p4.setBackground(Color.white);
        p4.setPreferredSize(new Dimension(100, 0));
        //俩个标签组件
        JLabel j1 = new JLabel("注册账号");
        j1.setForeground(new Color(100, 149, 238));
        j1.setBounds(0, 25, 60, 30);
        j1.setFont(new Font("宋体", Font.PLAIN, 12));
        p4.add(j1);
        JLabel j2 = new JLabel("找回密码");
        j2.setForeground(new Color(100, 149, 238));
        j2.setBounds(0, 70, 60, 30);
        j2.setFont(new Font("宋体", Font.PLAIN, 12));
        p4.add(j2);

        /*
            底部
         */
        JPanel p5 = new JPanel();
        p5.setPreferredSize(new Dimension(0, 51));
        p5.setLayout(null);
        p5.setBackground(Color.white);

        //设置按钮
        jb.setFont(new Font("宋体", Font.PLAIN, 13));
        jb.setBounds(130, 0, 175, 40);
        //文字放入图片中间
        jb.setHorizontalTextPosition(SwingConstants.CENTER);
        jb.setFocusPainted(false);
        jb.setContentAreaFilled(false);
        jb.setBorderPainted(false);
        jb.setRolloverIcon(new ImageIcon("C:/Users/lixiang/Desktop/images/login_normal.jpg"));
        p5.add(jb);
        jb.addActionListener(this);

        //2.布局
        jf.setLayout(new BorderLayout());

        //顶部
        jf.add(p1, BorderLayout.NORTH);

        //中部
        jf.add(p3, BorderLayout.CENTER);
        //左侧
        jf.add(p2, BorderLayout.WEST);
        //右侧
        jf.add(p4, BorderLayout.EAST);
        //底部
        jf.add(p5, BorderLayout.SOUTH);

        jf.setVisible(true);//设置可视化
    }


    //处理登陆按钮监听
    @Override
    public void actionPerformed(ActionEvent e) {

        //System.out.println("你点击了登陆按钮");
        if (e.getSource() == jb) {
            String username = jtf.getText();//接收的用户名.trim把后面的
            if (username.equals("")) {
                JOptionPane.showMessageDialog(this, "用户名不能为空，请重新输入！", "错误", JOptionPane.ERROR_MESSAGE);
            } else {
                //发送用户名给服务器端
                PrintStream ps = null;
                try {
                    ps = new PrintStream(new Socket("127.0.0.1", 9000).getOutputStream());
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                ps.println(username);
                //System.out.println("已经发送用户名出去了！");
                jf.dispose();//登陆成功以后关闭登陆面板
                new ChatClient().launchFrame(username);//跳转到聊天室界面
            }
        }
    }
}
